# Replication archive for: Alexander Coppock, Kimberly Gross, Ethan Porter, Emily Thorson, and Thomas J. Wood, 
# "Conceptual Replication of Four Key Findings about Factual Corrections and Misinformation during the 2020 US Election: Evidence from Panel-Survey Experiments." 
# Forthcoming in the British Journal of Political Science doi:10.1017/S0007123422000631

theme_tw <- theme(
  panel.background = element_rect(color = "grey95", fill = "grey95"),
  panel.grid = element_blank(),
  strip.text.y = element_text(angle = 0),
  strip.background = element_rect(color = "grey95", fill = "grey95"),
  legend.background = element_rect(color = "white", fill = "white"),
  legend.margin = margin(-.5, 0, 0, 0, "cm"),
  panel.grid.minor = element_blank(),
  plot.title = element_text(face = "bold", hjust = 0),
  plot.subtitle = element_text(size = 10),
  plot.caption = element_text(face = "italic", size = 7),
  legend.position = "bottom"
)


parse_nfc <- function(x, reverse = FALSE) {
  if (!reverse) {
    case_when(
      x == "Extremely uncharacteristic" ~ 1,
      x == "Somewhat uncharacteristic" ~ 2,
      x == "Uncertain" ~ 3,
      x == "Somewhat characteristic" ~ 4,
      x == "Extermely characteristic" ~ 5
    )
  } else{
    case_when(
      x == "Extremely uncharacteristic" ~ 5,
      x == "Somewhat uncharacteristic" ~ 4,
      x == "Uncertain" ~ 3,
      x == "Somewhat characteristic" ~ 2,
      x == "Extermely characteristic" ~ 1
    )
  }
}

parse_tipi <- function(x, reverse = FALSE) {
  if (!reverse) {
    case_when(
      x == "Disagree strongly" ~ 1,
      x == "Disagree moderately" ~ 2,
      x == "Disagree a little" ~ 3,
      x == "Neither agree nor disagree" ~ 4,
      x == "Extermely characteristic" ~ 5,
      x == "Agree moderately" ~ 6,
      x == "Agree strongly" ~ 7
    )
  } else{
    case_when(
      x == "Disagree strongly" ~ 7,
      x == "Disagree moderately" ~ 6,
      x == "Disagree a little" ~ 5,
      x == "Neither agree nor disagree" ~ 4,
      x == "Extermely characteristic" ~ 3,
      x == "Agree moderately" ~ 2,
      x == "Agree strongly" ~ 1
    )
  }
}


parse_belief <- 
  function(belief){
    case_when(belief == "Not at all accurate" ~ 1,
              belief == "Not very accurate" ~ 2,
              belief == "Somewhat accurate" ~ 3,
              belief == "Very accurate" ~ 4)
  }


  # OLD, WRONG!?
  recode_certainty <- 
    function(belief, certainty){
      case_when(belief %in% c("Not at all accurate", "Not very accurate") ~ 100 - certainty,
                belief %in% c("Somewhat accurate", "Very accurate") ~ certainty)
    }
  
  # NEW, RIGHT?
  recode_certainty <- 
    function(belief, certainty){
      case_when(belief %in% c("Not at all accurate", "Not very accurate") ~ 50 - 0.5 * certainty,
                belief %in% c("Somewhat accurate", "Very accurate") ~ 50 + 0.5 * certainty)
    }


add_parens <- function(x, digits = 3) {
  x <- as.numeric(x)
  return(paste0("(", sprintf(paste0("%.", digits, "f"), x), ")"))
}

format_num <- function(x, digits = 3) {
  x <- as.numeric(x)
  return(paste0(sprintf(paste0("%.", digits, "f"), x)))
}

make_se_entry <- function(est, se, digits = 2){
  paste0(format_num(est, digits = digits)," ", add_parens(se, digits = digits))
}

make_interval_entry <-
  function(conf.low, conf.high, digits = 2) {
    paste0(
      "[",
      format_num(conf.low, digits = digits),
      ", ",
      format_num(conf.high, digits = digits),
      "]"
    )
  }